/*
 * @Author: szx
 * @Date: 2022-03-06 13:53:40
 * @LastEditTime: 2022-03-06 14:22:37
 * @Description:
 * @FilePath: \leetcode\6000-6100\6018\6018.js
 */

function TreeNode(val, left, right) {
    this.val = val === undefined ? 0 : val;
    this.left = left === undefined ? null : left;
    this.right = right === undefined ? null : right;
}

/**
 * @param {number[][]} descriptions
 * @return {TreeNode}
 */
var createBinaryTree = function (descriptions) {
    const map = new Map();
    const set = new Set();
    for (let des of descriptions) {
        let parent;
        if (map.has(des[0])) parent = map.get(des[0]);
        else {
            parent = new TreeNode(des[0]);
            map.set(des[0], parent);
        }
        let child;
        if (map.has(des[1])) child = map.get(des[1]);
        else {
            child = new TreeNode(des[1]);
            map.set(des[1], child);
        }
        set.add(des[1]);
        if (des[2] == 1) parent.left = child;
        else parent.right = child;
    }
    for (let m of map) {
        if (!set.has(m[1].val)) {
            return m[1];
        }
    }
    return null;
};
